import { ref, watch } from "vue"

export function useInsertMethods (fieldList:any) {
  const allInsertCheckBox = ref(false)
  const insertCheckBoxList = ref([])
  const insertFieldList = ref<any>([])
  function changeInsertAllCheckBox (e: any) {
    if (e) {
      insertCheckBoxList.value = insertFieldList.value.map(
        (item: any) => item.value
      )
    }else{
      insertCheckBoxList.value = []
    }
  }

  /**
   * 初始化更新列表
   */
  function initInsertList () {
    let a = fieldList.value[0].fields.map((item: any) => {
      return {
        aliasName: '',
        ...item,
        isParamName: true
      }
    })
    insertFieldList.value = a as any
  }

  watch(
    () => insertCheckBoxList.value,
    newVal => {
      allInsertCheckBox.value =
        newVal.length === insertFieldList.value.length && newVal.length > 0
    },
    {
      deep: true,
      immediate: true
    }
  )
  return {
    allInsertCheckBox,
    insertCheckBoxList,
    insertFieldList,
    changeInsertAllCheckBox,
    initInsertList
  }
}
